filetoimport=paste("Derived Data/dfbartik2005_",40,"kmanalysis_annual.csv",sep="")

dfbartik=fread(filetoimport)



############### Bartik 2005 IV ################# 

  dfbartik_may=dfbartik[dfbartik$testmonth=="May",]
  


# do some correlations
df_cor=dfbartik_may

df_cor$pctbachelorhigher2005=as.numeric(df_cor$pctbachelorhigher2005)
df_cor$pctsinglemother2005=as.numeric(df_cor$pctsinglemother2005)

df_cor=unique(df_cor[,c("pctutility2005","pctmanufac2005","logmedianincome2005","stateabb","leaid","pct_white_2005","pct_asian_2005","pct_black_2005","pct_hisp_2005","pct_FRPM_2005","pct_speced_2005","pct_ell_2005","pctbachelorhigher2005","pctemploy2005","pctsinglemother2005","coalshare_40", "gasshare_40","renewableshare_40","oilshare_40")])

library(readxl)

state_div=read.csv("Raw Data/4. ACS Data/us census bureau regions and divisions.csv", stringsAsFactors = FALSE)
state_div$stateabb=state_div$State.Code
df_cor=merge(df_cor,state_div,by="stateabb")

# drop those with missings:
df_cor=df_cor %>% drop_na(pct_white_2005,pct_black_2005,pctemploy2005,pctsinglemother2005, pctutility2005, leaid)

# and drop those that have all fuel mixes missing:
x=which(is.na(df_cor$coalshare_40) & is.na(df_cor$gasshare_40) & is.na(df_cor$oilshare_40) & is.na(df_cor$renewableshare_40))

df_cor_nomissing=df_cor[!x,]
df_cor_nomissing$pctemploy2005=as.numeric(df_cor_nomissing$pctemploy2005)

df_corout=df_cor_nomissing %>%
  group_by(Region) %>%
  summarize(coal_wht=cor(pct_white_2005,coalshare_40),
            gas_wht=cor(pct_white_2005,gasshare_40),
            oil_wht=cor(pct_white_2005,oilshare_40),
            renew_wht=cor(pct_white_2005,renewableshare_40),
            coal_blk=cor(pct_black_2005,coalshare_40),
            gas_blk=cor(pct_black_2005,gasshare_40),
            oil_blk=cor(pct_black_2005,oilshare_40),
            renew_blk=cor(pct_black_2005,renewableshare_40),
            coal_hisp=cor(pct_hisp_2005,coalshare_40),
            gas_hisp=cor(pct_hisp_2005,gasshare_40),
            oil_hisp=cor(pct_hisp_2005,oilshare_40),
            renew_hisp=cor(pct_hisp_2005,renewableshare_40),
            coal_asian=cor(pct_asian_2005,coalshare_40),
            gas_asian=cor(pct_asian_2005,gasshare_40),
            oil_asian=cor(pct_asian_2005,oilshare_40),
            renew_asian=cor(pct_asian_2005,renewableshare_40),
            coal_FRPM=cor(pct_FRPM_2005,coalshare_40),
            gas_FRPM=cor(pct_FRPM_2005,gasshare_40),
            oil_FRPM=cor(pct_FRPM_2005,oilshare_40),
            renew_FRPM=cor(pct_FRPM_2005,renewableshare_40),
            coal_speced=cor(pct_speced_2005,coalshare_40),
            gas_speced=cor(pct_speced_2005,gasshare_40),
            oil_speced=cor(pct_speced_2005,oilshare_40),
            renew_speced=cor(pct_speced_2005,renewableshare_40),
            coal_ell=cor(pct_ell_2005,coalshare_40),
            gas_ell=cor(pct_ell_2005,gasshare_40),
            oil_ell=cor(pct_ell_2005,oilshare_40),
            renew_ell=cor(pct_ell_2005,renewableshare_40),
            coal_pctbachelorhigher=cor(pctbachelorhigher2005,coalshare_40),
            gas_pctbachelorhigher=cor(pctbachelorhigher2005,gasshare_40),
            oil_pctbachelorhigher=cor(pctbachelorhigher2005,oilshare_40),
            renew_pctbachelorhigher=cor(pctbachelorhigher2005,renewableshare_40),
            coal_pctemploy=cor(pctemploy2005,coalshare_40),
            gas_pctemploy=cor(pctemploy2005,gasshare_40),
            oil_pctemploy=cor(pctemploy2005,oilshare_40),
            renew_pctemploy=cor(pctemploy2005,renewableshare_40),
            coal_pctsinglemother=cor(pctsinglemother2005,coalshare_40),
            gas_pctsinglemother=cor(pctsinglemother2005,gasshare_40),
            oil_pctsinglemother=cor(pctsinglemother2005,oilshare_40),
            renew_pctsinglemother=cor(pctsinglemother2005,renewableshare_40),
            coal_pctmanufac=cor(pctmanufac2005,coalshare_40),
            gas_pctmanufac=cor(pctmanufac2005,gasshare_40),
            oil_pctmanufac=cor(pctmanufac2005,oilshare_40),
            renew_pctmanufac=cor(pctmanufac2005,renewableshare_40),
            coal_pctutility=cor(pctutility2005,coalshare_40),
            gas_pctutility=cor(pctutility2005,gasshare_40),
            oil_pctutility=cor(pctutility2005,oilshare_40),
            renew_pctutility=cor(pctutility2005,renewableshare_40),
            coal_logmedianincome=cor(logmedianincome2005,coalshare_40),
            gas_logmedianincome=cor(logmedianincome2005,gasshare_40),
            oil_logmedianincome=cor(logmedianincome2005,oilshare_40),
            renew_logmedianincome=cor(logmedianincome2005,renewableshare_40)
  ) %>% ungroup()


dfcoravg=df_corout %>% summarise_if(is.numeric, mean, na.rm = TRUE)
library(pracma)
dfcormatout=t(Reshape(as.matrix(dfcoravg),4,13))

colnamesvec=c("% White", "% Black", "% Hisp","% Asian","% FRL", "% Spec Ed", "% ELL", "% Bachelor", "% Employ","% Single Mother","% Manufacturing","% Utility","Median Income")

dfcormatoutnam=cbind(colnamesvec,(dfcormatout))

dfcormatoutnam[,2:5] <- round(as.numeric(dfcormatoutnam[,2:5]), 3)

stargazer(dfcormatoutnam, title="",notes="", digits=3, out="output/cor_mat.tex")

